<?php
class goods extends Activity {
    function indexTask($id,$fromUid){
        $um=UserModule::init('user');
        $goods=GoodsMode::init();
        $cats=MenuModule::init();
        $data=$goods->getDetailInfo($id);// gopen id
        if(empty($data)) return new Intent(404,"商品未找到");
        //添加到浏览历史
        if($um->checkLogin()){
            $user_id=$um->getLoginId();
            UserMode::init()->addBrowse($user_id,$id);
            //分享浏览历史
            if(isset($fromUid)&&!empty($fromUid)&&$fromUid!=$user_id)
                $goods->goodsShareAdd($data['gopen_id'],$user_id,$fromUid);
        }
        $data['cat']=$cats->getParents($data['goods_cat_id']);
        $fInfo=$cats->getFather($data['goods_cat_id']);
        $data['category']=$cats->getAll((int)$fInfo['id']);
        usort($data['category'],function($a,$b){
            return -$a['point']+$b['point'];
        });
        $data['shop']['addressLink']=AddressMode::init()->getDetailAsString($data['shop']['address_id']);
        switch($data['type']){
            case "service": $this->service($data); break;
            case "commodity": $this->commodity($data); break;
            case "non_sale": $this->non_sale($data); break;
            case "normal": default: $this->normal($data); break;
        }
        return null;
    }

    function normal($data){
        View::displayAsHtml($data, "goods-normal.php");
    }
    function service($data){
        View::displayAsHtml($data, "goods-service.php");
    }
    function commodity($data){
        View::displayAsHtml($data, "goods-commodity.php");
    }
    function non_sale($data){
        View::displayAsHtml($data, "goods-non-sale.php");
    }

    // 获取所有价格的接口，参数是gopen id
    function allPriceTask($id){
        $goods=GoodsMode::init();
        $r['data']['list']=$goods->getAllPrice($id);
        $r['state']=DataMessage::STATE_SUCCESS;
        View::displayAsJson($r);
    }

    function labelTask($label_id,$orderBy,$asc,$page){
        $goods=GoodsMode::init();
        $label_id=(int)$label_id;
        $asc = $asc==1?true:false;
        if(!empty($orderBy)){
            $r[$orderBy]=$asc;
        }
        // 现有的参数
        $r['paramRemain']=array('label_id'=>$label_id);
        $page=empty($page)||$page<0 ? 1:(int)$page;
        $countOfPage=16;
        $offset=($page-1)*$countOfPage;
        list($r['goods'],$total)=GoodsMode::init()->getList(null,$label_id,null,null,$orderBy,$asc,$countOfPage,$offset);
        if($total!=0 && !empty($r['goods'])){
            $r['list']=$goods->getDetailInfo($r['goods']);
            $goods->sortGoods($r['list'], $orderBy, $asc);
        }else{
            $r['list']=array();
        }
        $url=WebRouter::init()->getQuestion("page=");
        $r['page']=new Page($page,$total,$countOfPage);
        $r['page']->setPageCallback(function($page)use($url,$label_id,$orderBy,$asc){
            return $url.$page."&label_id=".$label_id."&orderBy=".$orderBy."&asc=".$asc;
        });
        $r['category']=MenuModule::init()->getAll(0);
        usort($r['category'],function($a,$b){
            return -$a['point']+$b['point'];
        });
        $labelData=LabelModule::init()->getDetail($label_id);
        $r['pageTitle']=isset($labelData['name'])&&!empty($labelData['name']) ? $labelData['name']:"未选择标签";
        $r['is_bottom']=false;
        $r['pageEmpty']=$total==0?true:false;
        View::displayAsHtml($r, "goods-list.php");
    }

    function categoryTask($cat_id,$orderBy,$asc,$page){
        $goods=GoodsMode::init();
        $cats=MenuModule::init();
        $asc = $asc==1?true:false;
        if(!empty($orderBy)){
            $r[$orderBy]=$asc;
        }
        // 现有的参数
        $r['cat_id']=$cat_id=(int)$cat_id;
        $r['paramRemain']=array('cat_id'=>$r['cat_id']);
        //分类级别
        $r['cat']=$cats->getParents($cat_id);
        $page=empty($page)||$page<0 ? 1:(int)$page;
        $countOfPage=16;
        $offset=($page-1)*$countOfPage;
        if($cat_id!=0){
            $pos=$cats->getPosterity($cat_id);
            $pos_ids=is_array($pos)?implode(',',$pos):$pos;
        }else{
            $pos_ids=null;
        }
        list($r['goods'],$total)=$goods->getList(null,null,$pos_ids,null,$orderBy,$asc,$countOfPage,$offset);
        if($total!=0 && !empty($r['goods'])){
            $r['list']=$goods->getDetailInfo($r['goods']);
            $goods->sortGoods($r['list'], $orderBy, $asc);
        }else{
            $r['list']=array();
        }
        $url=WebRouter::init()->getQuestion("page=");
        $r['page']=new Page($page,$total,$countOfPage);
        $r['page']->setPageCallback(function($page)use($url,$cat_id,$orderBy,$asc){
            return $url.$page."&cat_id=".$cat_id."&orderBy=".$orderBy."&asc=".$asc;
        });
        $M=MenuModule::init();
        $r['category']=$M->getAll($cat_id);
        if(empty($r['category'])){
            $fInfo=$M->getFather($cat_id);
            $r['category']=$M->getAll((int)$fInfo['id']);
        }
        usort($r['category'],function($a,$b){
            return -$a['point']+$b['point'];
        });
        $r['categoryRoot']=$M->getDetail($cat_id);
        $r['is_bottom']=$M->getBottom($cat_id)==$cat_id ? true:false;
        $r['pageEmpty']=$total==0?true:false;
        View::displayAsHtml($r, "goods-list.php");
    }
    function newTask(){
        list($r['goods'],$total)=GoodsMode::init()->getList(null,null,null,null,'time',false,16,0);
        if($total!=0 && !empty($r['goods'])){
            $r['list']=GoodsMode::init()->getDetailInfo($r['goods']);
        }else{
            $r['list']=array();
        }
        GoodsMode::init()->sortGoods($r['list'], "create_time", false);
        View::displayAsHtml($r, "goods-new.php");
    }
    function cartTask(){
        $result=array();
        View::displayAsHtml($result, "cart.php");
    }

}